package com.peas.platform.module.icims.base.approve.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.approve.adapter.repository.ApproveOrderRepository;
import com.peas.platform.module.icims.base.approve.adapter.repository.po.ApproveOrderPo;
import com.peas.platform.module.icims.base.approve.domain.ApproveOrderDomain;
import com.peas.platform.module.icims.base.approve.dto.ApproveOrderDto;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class ApproveOrderDomainImpl extends BaseDomainImpl<ApproveOrderDto, ApproveOrderPo> implements ApproveOrderDomain {

    @Resource
    private ApproveOrderRepository approveOrderRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<ApproveOrderPo> getMapper() {
        return approveOrderRepository;
    }

    @Override
    public Class<? extends BaseRepository<ApproveOrderPo>> getRepositoryClass() {
        return ApproveOrderRepository.class;
    }

    @Transactional
    @Override
    public int save(ApproveOrderDto dto) {
        Assert.notNull(dto.getApproveId(), "请选择审批模版");
        if (StrUtil.isBlank(dto.getApproveOrderCode())) {
            String code = sequenceService.genSequence("base", "ApproveOrderDto", "AO", 4, 1);
            dto.setApproveOrderCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("approveOrderCode", dto.getApproveOrderCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(ApproveOrderDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("approveOrderCode", dto.getApproveOrderCode());
        return result;
    }

}
